// my/my.js
import {
  AccountModel
} from '../../../models/account'

import {
  Format
} from '../../../utils/common'
const accountmodel = new AccountModel()
const app = getApp()
let last
const format = new Format()
Page({
  /**
   * 页面的初始数据
   */
  props: { // 组件传参默认值
    className: '', // 父级加入className
    verifyCodeLength: 6, // 验证码长度
    numberCode: true, // 是否是是数字键盘
    codeTime: 60, // 倒计时时间
    disabled: false, // 手机号是否可以编辑
    onSendCode: () => { }, // 点击发送验证码
    onCodeInput: () => { } // 输入验证码事件
  },
  data: {
    phone: '', // 获取的手机号
    btnName: '发送验证码', // 按钮显示名称
    canSend: false, // 是否可以发送验证吗
    hasSend: false, // 是否已经发送
    mobileValue: '', // 组件手机号的值
    mobileSafe: '', // 脱敏后的值
    txt: '', // 文本赋值
    mobileDisabled: false, // 手机号是否禁止输入
    btnDisabled: true, //提交按钮是否禁用
    code: '',
    openTipPop: false, // 是否显示弹窗
    TipTxt: '', // 弹窗内容
    tips: '', // 点击按钮方法
    checkBox: false, // 服务协议选中
    openrePop: false,
    href: '', //页面跳转
    TipTxtTwo: {
      tittle: '登陆成功，马上充值即享会员卡支付优惠',
      leftBtn: '再看看',
      rightBtn: '立即充值'
    }, // 位置授权拒绝后弹窗内容
    openTipPopTwoBtn: false, //是否显示位置授权拒绝后弹窗
     onCounterPlusOne: false, // 注册送活动弹窗
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options && options.href) {
      this.setData({
        href: options.href,
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  /**
   * 点击清除手机号
   * @method onMobileClear
   */
  onMobileClear() {
    this.setData({
      mobileValue: '',
      mobileSafe: '',
      canSend: false,
      hasSend: false,
      // btnName: '发送验证码'
    });
  },
  /**
   * 点击获取验证码触发
   * @method onSendCode
  */
  onSendCode() {
    if (this.data.btnName == '重新发送' || this.data.btnName == '发送验证码') {
      if (this.data.canSend) {
        const result = this.verifyTel(this.data.mobileValue);
        console.log(result)
        if (!result) {
          my.showToast({
            type: 'none',
            content: '请输入正确的手机号码',
            duration: 1000,
          });
          return
        }
        const {
          mobileValue
        } = this.data;
        accountmodel.mobileVerfiCode(mobileValue).then(res => {
          console.log(res)
          if (res) {
            if (res.code == '00') {
              this.settimer();
              const pat = /(\d{3})\d*(\d{4})/;
              let str = mobileValue.replace(pat, '$1****$2');
              this.props.onSendCode({
                mobile: mobileValue
              });
              this.setData({
                mobileSafe: str,
                hasSend: true,
                txt: '已',
                mobileDisabled: true,
                canSend: false
              });
            } else if (res.message) {
              this.setData({
                openTipPop: true,
                TipTxt: res.message,
                tips: 'close',
              });
            }
          } else {
            my.showToast({
              type: 'none',
              content: '获取验证码失败',
              duration: 1000,
            });
          }
        })
      } else {
        const {
          popoverShow,
          hasSend
        } = this.data;
        if (!hasSend && !popoverShow) {
          my.showToast({
            type: 'none',
            content: '请输入正确的手机号码',
            duration: 1000,
          });
        }
      }
    } else {

    }
  },
  /**
   * 输入验证码触发事件并传给父
   * @method onCodeInput
   * @param {*} e
   */
  onCodeInput(e) {
    if (e.detail.value.length == 6) {
      this.setData({
        verifyCode: e.detail.value,
      })
      if (this.data.mobileValue && this.data.verifyCode) {
        this.setData({
          btnDisabled: false,
        })
      } else {
        this.setData({
          btnDisabled: true,
        })
      }
    } else {
      this.setData({
        btnDisabled: true
      })
    }
    // this.props.onCodeInput && this.props.onCodeInput(e);
  },
  /**
   * 正则校验 获取值 改变
   * @method onMobileInput
   * @param {*} e
   */
  onMobileInput(e) {
    const {
      value
    } = e.detail;
    const result = this.verifyTel(value);
    if (result) { // 11位数字
      this.setData({
        mobileValue: value,
        canSend: true,
        // btnName: '发送验证码'
      });
    } else {
      this.setData({
        btnDisabled: true,
        canSend: false,
        mobileValue: value,
        // btnName: '发送验证码'
      });
    }
  },
  bindKeyInput(e) {
    if (this.data.mobileValue && this.data.verifyCode) {
      this.setData({
        btnDisabled: false,
      })
    } else {
      this.setData({
        btnDisabled: true,
      })
    }
  },
  /**
   * 倒计时定时器
   * @method settimer
   */
  settimer() {
    let timer;
    let codeTime = this.props.codeTime;
    const {
      disabled
    } = this.props;
    clearInterval(timer);
    timer = setInterval(() => {
      codeTime--;
      this.setData({
        btnName: `倒计时${codeTime}秒`
      });
      if (codeTime === 0) {
        clearInterval(timer);
        const box = this.data.othervalue;
        this.setData({
          btnName: '重新发送',
          // mobileDisabled: disabled ? true : false,
          canSend: true,
          txt: ''
        });
      }
    }, 1000);
  },
  /**
   * 检验手机号是否正确
   * @method verifyTel
   * @param {*} number // 手机号
   */
  verifyTel(number) { // 手机号正则校验
    const reg = /^1[3|4|5|7|8|9]\d{9}$/;
    return reg.test(number);
  },
  login () {
    let vm = this
    accountmodel.loginMember(this.data.mobileValue,this.data.verifyCode).then(res => {
      console.log(res)
      if (res) {
        if (res.code == '00') {
          app.globalData.user.activityStatus = res.status
          app.globalData.user.cardAsn = res.result.currentAsn
          app.globalData.user.phone = res.result.mobilePhone
          if (vm.data.href ==  'oilConfirm'  || vm.data.href ==  'oilResult') {
            vm.setData({
              'TipTxtTwo[tittle]': '登陆成功，马上充值即享会员卡支付优惠',
              'openTipPopTwoBtn': true,
            })
            return
          }
          vm.setData({
            openTipPop: true,
            TipTxt: '登录成功',
            tips: 'href',
          });
        } else if (res.code == '03' && (res.message).indexOf("会员不存在")!=-1) {
          this.setData({
            openrePop: true,
          });
        } else  if (res.message){
          this.setData({
            openTipPop: true,
            TipTxt: res.message,
            tips: 'close',
          });
        }
      } else {
        this.setData({
          openTipPop: true,
          TipTxt: '登录失败',
          tips: 'close',
        });
      }
    })
  },
  // 点击知道了
  tipPopSure(tips) {
    this.setData({
      openTipPop: false
    });
    if (tips == 'href') {
      my.navigateBack('1')
    }
  },
  //  点击注册协议
  onChange(e) {
    console.log(e);
    this.setData({
      checkBox: e.detail.value,

    });
  },
  register () {
    let vm = this
    if (!this.data.checkBox) {
        my.showToast({
          type: 'none',
          content: '请同意《北京壳牌会员服务协议》',
          duration: 1000,
        });
        return
    }
    accountmodel.registered(this.data.mobileValue,this.data.verifyCode).then(res => {
      console.log(res)
      if (res) {
        if (res.code == '00') {
          app.globalData.user.cardAsn = res.result.asn
          app.globalData.user.phone = res.result.mobilePhone
          app.globalData.user.activityStatus = 1
          app.globalData.user.openTicketType = 2
          vm.getCouponListByOrderIdOrOpenId()
        } else if (res.message) {
          this.setData({
            openTipPop: true,
            TipTxt: res.message,
            tips: 'close',
          });
        }
      } else {
        this.setData({
          openTipPop: true,
          TipTxt: '注册会员失败',
          tips: 'close',
        });
      }
    })
  },
  //  获取注册送接口
  getCouponListByOrderIdOrOpenId () {
    let vm= this
    accountmodel.getCouponListByOrderIdOrOpenId('1','').then(res => {
      console.log(res)
      if (res && res.data && res.data.length > 0) {
        vm.setData({
          Registeractive: res,
          openRegisterCouponPop: true,
        })
      } else {
        if (vm.data.href ==  'oilConfirm'  || vm.data.href ==  'oilResult') {
          vm.setData({
            openrePop: false,
            'TipTxtTwo[tittle]': '恭喜您成为北京壳牌会员，马上充值即享会员卡支付优惠',
            'openTipPopTwoBtn': true,
          })
          return
        }
        this.setData({
          openTipPop: true,
          TipTxt: '注册成功',
          tips: 'href',
        });
      }
    })
  },

  // 关闭用户协议接口
  closerepop () {
    this.setData({
      openrePop: false,
      mobileDisabled: false,
      mobileSafe: '',
      canSend: false,
      hasSend: false,
      verifyCode:'',
      btnDisabled: true,
    });
  },
  // 协议
  TermsService() {
    my.navigateTo({
        url: '/pages/account/agreement/agreement'
      })
  },
  // 关闭双按钮弹窗
  tipPopCloseTwoBtn() {
    this.setData({
      openTipPopTwoBtn: false,
    });
    my.navigateBack('1')
  },
  // 跳转充值
  tipPopSureTwoBtn() {
    let vm = this
    my.redirectTo({
      url: '/pages/recharge/recharge/recharge?href=oilConfirm',
      success: () => {
        vm.setData({
          openTipPopTwoBtn: false
        });
      }
    })
  },
  // 关闭充值送弹窗
  onCloseRegisterCouponPop() {
    let vm = this
    this.setData({
        openRegisterCouponPop: false,
    })
    if (vm.data.href ==  'oilConfirm'  || vm.data.href ==  'oilResult') {
      vm.setData({
        openrePop: false,
        'TipTxtTwo[tittle]': '恭喜您成为北京壳牌会员，马上充值即享会员卡支付优惠',
        'openTipPopTwoBtn': true,
      })
      return
    }
    this.setData({
      openTipPop: true,
      TipTxt: '注册成功',
      tips: 'href',
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})